﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emoney.UserTraining.Model;
using NHibernate.Linq;

namespace Emoney.UserTraining.DAL
{
    public class Traning_NetworkMeetingDAL
    {

        /// <summary>
        /// 课程表的数据（未开始的课程（分页））
        /// </summary>
        /// <returns></returns>
        public List<Traning_NetworkMeeting> GetCourse(Traning_NetworkMeeting_Query query)
        {
            string sql = @"select [Id],[mtg_name],[mtg_conent],[txtteachar],[txtreateachar],[host_key],[mtg_password]
      ,[txt_phone],[mtg_view],[email_flag],[use_num],[host_id],[pres_id],[allperson],[pri_name],[radDisplayType]
      ,[ddlarea],[chblist_pro],[click_num],[time_flag],[class_date],[Duration_date]
      ,[before_time],[period],[startDate],[endDate],[max_audio_chan],[max_video_chan],[max_video_size]
      ,[def_video_size],[video_frame_rate],[max_bandwidth],[maliu],[mtg_mode],[video_public]
      ,[layout_mode],[video_layer],[funSeting],[createtime],[updatetime],[satisfaction],[isCreateMeeting],[user_type]
      ,[meeting_type],[isRecommend],[video_Recommend],[video_url],[lecturerImage],isnull(mtg_id,'') mtg_id
from
(select *,RANK() over(partition by mtg_name,txtteachar,mtg_conent,txtreateachar order by class_date asc,id asc) as GroupIndex
	from Traning_NetworkMeeting with(nolock)
	where dateadd(mi, CONVERT(int,duration_date),class_date)>=GETDATE() and allperson=1) as t
where GroupIndex=1 order by class_date asc";
            IEnumerable<Traning_NetworkMeeting> data = SqlHelper.GetDataByColumnsLst<Traning_NetworkMeeting>(DBConn.Temporary, sql);
            if (query.StartDate != null)
            {
                data = data.Where(p => p.class_date >= query.StartDate);
            }
            if (query.EndDate != null && query.EndDate != DateTime.MinValue)
            {
                query.EndDate = query.EndDate.AddDays(1).Date;
                data = data.Where(x => x.class_date < query.EndDate);
            }

            if (!string.IsNullOrEmpty(query.keywords))
            {
                data = data.Where(p => p.mtg_name.Contains(query.keywords) || p.txtteachar.Contains(query.keywords) || p.mtg_id.Contains(query.keywords));
            }
            return BaseDAL.Paging<Traning_NetworkMeeting>(data, query).ToList();
        }

        /// <summary>
        /// 会议未开始的数据
        /// </summary>
        /// <returns></returns>
        public List<Traning_NetworkMeeting> GetAll()
        {
            string sql = @"select [Id],[mtg_name],[mtg_conent],[txtteachar],[txtreateachar],[host_key],[mtg_password]
      ,[txt_phone],[mtg_view],[email_flag],[use_num],[host_id],[pres_id],[allperson],[pri_name],[radDisplayType]
      ,[ddlarea],[chblist_pro],[click_num],[time_flag],[class_date],[Duration_date]
      ,[before_time],[period],[startDate],[endDate],[max_audio_chan],[max_video_chan],[max_video_size]
      ,[def_video_size],[video_frame_rate],[max_bandwidth],[maliu],[mtg_mode],[video_public]
      ,[layout_mode],[video_layer],[funSeting],[createtime],[updatetime],[satisfaction],[isCreateMeeting],[user_type]
      ,[meeting_type],[isRecommend],[video_Recommend],[video_url],[lecturerImage],isnull(mtg_id,'') mtg_id
from
(select *,RANK() over(partition by mtg_name,txtteachar,mtg_conent,txtreateachar order by class_date asc,id asc) as GroupIndex
	from Traning_NetworkMeeting with(nolock)
	where dateadd(mi, CONVERT(int,duration_date),class_date)>=GETDATE() and allperson=1) as t
where GroupIndex=1 order by class_date asc";
            return SqlHelper.GetDataByColumnsLst<Traning_NetworkMeeting>(DBConn.Temporary, sql);
        }

        public List<Traning_MeetingType> GetMeetingType()
        {
            string sql = "select distinct meeting_type as meetingType from Traning_NetworkMeeting where [video_Recommend]=1 and meeting_type<>'' ";
            return SqlHelper.GetDataByColumnsLst<Traning_MeetingType>(DBConn.Temporary, sql);
        }
        /// <summary>
        /// 查询三年内历史数据
        /// </summary>
        /// <returns></returns>
        public List<Traning_NetworkMeeting> GetHistryList(Traning_NetworkMeeting_Query query)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                DateTime tady = DateTime.Now.AddYears(-3);
                DateTime tn = DateTime.Now;
                var data = context.Traning_NetworkMeeting;
                data = data.Where(p => p.endDate > tady);
                data = data.Where(p => p.class_date < tn);
                if (query.StartDate != null)
                {
                    data = data.Where(p => p.class_date >= query.StartDate);
                }
                if (query.EndDate != null && query.EndDate != DateTime.MinValue)
                {
                    query.EndDate = query.EndDate.AddDays(1).Date;
                    data = data.Where(x => x.class_date < query.EndDate);
                }

                if (!string.IsNullOrEmpty(query.keywords))
                {
                    data = data.Where(p => p.mtg_name.Contains(query.keywords) || p.txtteachar.Contains(query.keywords) || p.mtg_id.Contains(query.keywords));
                }
                return BaseDAL.Paging<Traning_NetworkMeeting>(data, query).ToList();
            }
        }

        /// <summary>
        /// 查询三年内历史数据(包括正在开始)
        /// </summary>
        /// <returns></returns>
        public List<Traning_NetworkMeeting> GetHistryList()
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                DateTime tady = DateTime.Now.AddYears(-3);
                DateTime tn = DateTime.Now;
                var data = context.Traning_NetworkMeeting;
                data = data.Where(p => p.endDate > tady);
                data = data.Where(p => p.class_date < tn);
                return data.ToList();
            }
        }

        /// <summary>
        /// 查询三年内数据
        /// </summary>
        /// <returns></returns>
        public List<Traning_NetworkMeeting> GetList(Traning_NetworkMeeting_Query query)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                DateTime tady = DateTime.Now.AddYears(-3);
                var data = context.Traning_NetworkMeeting;
                data = data.Where(p => p.endDate > tady);
                if (query.StartDate != null)
                {
                    data = data.Where(p => p.class_date >= query.StartDate);
                }
                if (query.EndDate != null && query.EndDate != DateTime.MinValue)
                {
                    query.EndDate = query.EndDate.AddDays(1).Date;
                    data = data.Where(x => x.class_date < query.EndDate);
                }

                if (!string.IsNullOrEmpty(query.keywords))
                {
                    data = data.Where(p => p.mtg_name.Contains(query.keywords) || p.txtteachar.Contains(query.keywords) || p.mtg_id.Contains(query.keywords));
                }
                return BaseDAL.Paging<Traning_NetworkMeeting>(data, query).ToList();
            }
        }
        /// <summary>
        /// 查询数据（查询当月所有网络会议）
        /// </summary>
        /// <returns></returns>
        public List<Traning_NetworkMeeting> GetMonthLst(Traning_NetworkMeeting_Query query)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                DateTime tady = DateTime.Now;
                var data = context.Traning_NetworkMeeting;
                data = data.Where(x => x.class_date.Month == tady.Month && x.class_date.Year == tady.Year);
                return data.ToList();
            }
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        public List<Traning_NetworkMeeting> GetList(string mtg_id)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                DateTime tady = DateTime.Now.AddMonths(-3);
                var data = context.Traning_NetworkMeeting;
                data = data.Where(x => x.endDate > tady);
                data = data.Where(x => x.mtg_id == mtg_id);
                return data.ToList();
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        public List<Traning_NetworkMeeting> GetListBy(string click_num)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                DateTime tady = DateTime.Now;
                var data = context.Traning_NetworkMeeting;
                data = data.Where(x => x.click_num == click_num);
                data = data.Where(x => x.class_date > tady);
                return data.ToList();
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        public Traning_NetworkMeeting GetModel(int id)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                var data = context.Traning_NetworkMeeting;
                return data.FirstOrDefault(p => p.Id == id);
            }
        }

        /// <summary>
        /// 新增和修改
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public int AddUpdate(Traning_NetworkMeeting c)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                if (c.Id != null)
                {
                    var vv = GetModel(c.Id);
                    //if (vv != null)
                    //{
                    //    c.Gensee_URL = vv.Gensee_URL;
                    //}
                }
                SetValueToOldNetmeeting(c);//数据同时插入在老的网络会议表（只做录入，不做修改）

                context.Session.SaveOrUpdate(c);//入新表
                context.Session.Flush();
                return 1;
            }
        }
        public int SetValueToOldNetmeeting(Traning_NetworkMeeting c)
        {
            using (TemporaryContext context1 = new TemporaryContext())
            {
                NetworkMeeting o = new NetworkMeeting();
                o.voip_meeting = c.voip_meeting;
                o.mtg_name = c.mtg_name;
                o.mtg_conent = c.mtg_conent;
                o.txtteachar = c.txtteachar;
                o.txtreateachar = c.txtreateachar;
                o.host_key = c.host_key;
                o.mtg_password = c.mtg_password;
                o.txt_phone = c.txt_phone;
                o.mtg_view = c.mtg_view;
                o.email_flag = c.email_flag;
                o.use_num = c.use_num;
                o.host_id = c.host_id;
                o.pres_id = c.pres_id;
                o.allperson = c.allperson;
                o.pri_name = c.pri_name;
                o.radDisplayType = c.radDisplayType;
                o.ddlarea = c.ddlarea;
                o.chblist_pro = c.chblist_pro;
                o.click_num = c.click_num;
                o.time_flag = c.time_flag;
                o.class_date = c.class_date.ToString();
                o.Duration_date = c.Duration_date;
                o.before_time = c.before_time;
                o.max_audio_chan = c.max_audio_chan;
                o.max_video_chan = c.max_video_chan;
                o.max_video_size = c.max_video_size;
                o.def_video_size = c.def_video_size;
                o.video_frame_rate = c.video_frame_rate;
                o.max_bandwidth = c.max_bandwidth;
                o.maliu = c.maliu;
                o.mtg_mode = c.mtg_mode;
                o.video_public = c.video_public;
                o.layout_mode = c.layout_mode;
                o.video_layer = c.video_layer;
                o.funSeting = c.funSeting;
                o.createtime = c.createtime;
                o.updatetime = c.updatetime;
                o.satisfaction = c.satisfaction;
                o.isCreateMeeting = c.isCreateMeeting;
                o.mtg_id = c.mtg_id;
                o.Gensee_URL = c.Gensee_URL;

                if (c.Id == null || c.Id == 0)
                {
                    context1.Session.SaveOrUpdate(o);
                    context1.Session.Flush();
                }
            }
            return 1;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(string id)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                int row = context.Session.Delete("from Traning_NetworkMeeting where Id='" + id + "'");
                context.Session.Flush();
                return row;
            }
        }

        /// <summary>
        /// 结束会议
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int deleteAllperson(int id)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                try
                {
                    var tb = context.Traning_NetworkMeeting;
                    var c = tb.FirstOrDefault(p => p.Id == id);
                    c.allperson = "2";
                    context.Session.SaveOrUpdate(c);
                    context.Session.Flush();
                    return 1;
                }
                catch
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 推荐和取消推荐
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Recommend(int id, string isRecommend)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                try
                {
                    var tb = context.Traning_NetworkMeeting;
                    var c = tb.FirstOrDefault(p => p.Id == id);
                    c.isRecommend = isRecommend;
                    context.Session.SaveOrUpdate(c);
                    context.Session.Flush();
                    return 1;
                }
                catch
                {
                    return 0;
                }
            }
        }


        /// <summary>
        /// 视频推荐和取消推荐
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int VideoRecommend(int id, string isRecommend)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                try
                {
                    var tb = context.Traning_NetworkMeeting;
                    var c = tb.FirstOrDefault(p => p.Id == id);
                    c.video_Recommend = isRecommend;
                    context.Session.SaveOrUpdate(c);
                    context.Session.Flush();
                    return 1;
                }
                catch
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 视频维护
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateNetworkMeeting(Traning_NetworkMeeting model)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                try
                {
                    var tb = context.Traning_NetworkMeeting;
                    var c = tb.FirstOrDefault(p => p.Id == model.Id);
                    c.video_Recommend = model.video_Recommend;
                    c.video_url = model.video_url;
                    context.Session.SaveOrUpdate(c);
                    context.Session.Flush();
                    return 1;
                }
                catch
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="checknum"></param>
        /// <returns></returns>
        public int deletetochecknum(string checknum,string updatetype)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                int row = 0;
                if (updatetype == "history")
                {
                    //历史会议
                    row = context.Session.Delete("from Traning_NetworkMeeting where dateadd(mi, CONVERT(int,duration_date),class_date)<GETDATE() and endDate<GETDATE() and allperson=1 and click_num='" + checknum + "'");
                }
                else
                {
                    //未开始会议
                    row = context.Session.Delete("from Traning_NetworkMeeting where dateadd(mi, CONVERT(int,duration_date),class_date)>=GETDATE() and allperson=1 and click_num='" + checknum + "'");
                }
                    context.Session.Flush();
                return row;
            }
        }

        /// <summary>
        /// 更新会议编号
        /// </summary>
        /// <param name="id"></param>
        /// <param name="mtg_id"></param>
        public void Modifymtg_id(int id, string mtg_id)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                var tb = context.Traning_NetworkMeeting;
                var c = tb.FirstOrDefault(p => p.Id == id);
                c.mtg_id = mtg_id;
                context.Session.SaveOrUpdate(c);
                context.Session.Flush();
            }
        }
        //得到所有的会场
        public List<Traning_NetworkMeeting> GetAllList()
        {
            IEnumerable<Traning_NetworkMeeting> data = GetHistryList();
            data = data.Union(GetAll());
            return data.ToList();
            //using (TemporaryContext context = new TemporaryContext())
            //{
            //    var data=context.Traning_NetworkMeeting;
            //    return data.Cacheable().ToList();
            //}
        }
        public List<Traning_NetworkMeeting> GetMeetByType(string meetType)
        {
            using (TemporaryContext context = new TemporaryContext())
            {

                return context.Traning_NetworkMeeting.Where(x => x.meeting_type == meetType && x.video_Recommend == "1").OrderByDescending(x => x.class_date).Cacheable().ToList();
            }
        }
    }
}
